home *** CD-ROM | disk | FTP | other *** search
- Imports System.ServiceProcess
-
- Public Class Encryptor
- Inherits System.ServiceProcess.ServiceBase
-
- #Region " Component Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- ' This call is required by the Component Designer.
- InitializeComponent()
-
- ' Add any initialization after the InitializeComponent() call
-
- End Sub
-
- ' The main entry point for the process
- Shared Sub Main()
- Dim ServicesToRun() As System.ServiceProcess.ServiceBase
-
- ' More than one NT Service may run within the same process. To add
- ' another service to this process, change the following line to
- ' create a second service object. For example,
- '
- ' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
- '
- ServicesToRun = New System.ServiceProcess.ServiceBase() {New Encryptor()}
-
- System.ServiceProcess.ServiceBase.Run(ServicesToRun)
- End Sub
- Friend WithEvents FileSystemWatcher1 As System.IO.FileSystemWatcher
-
- 'Required by the Component Designer
- Private components As System.ComponentModel.Container
-
- ' NOTE: The following procedure is required by the Component Designer
- ' It can be modified using the Component Designer.
- ' Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.FileSystemWatcher1 = New System.IO.FileSystemWatcher()
- CType(Me.FileSystemWatcher1, System.ComponentModel.ISupportInitialize).BeginInit()
- '
- 'FileSystemWatcher1
- '
- Me.FileSystemWatcher1.NotifyFilter = ((System.IO.NotifyFilters.FileName Or System.IO.NotifyFilters.DirectoryName) _
- Or System.IO.NotifyFilters.LastWrite)
- '
- 'Encryptor
- '
- Me.CanPauseAndContinue = True
- Me.ServiceName = "Encryptor"
- CType(Me.FileSystemWatcher1, System.ComponentModel.ISupportInitialize).EndInit()
-
- End Sub
-
- #End Region
-
- ' The path of the watched directory.
- ' IMPORTANT: create this directory if it doesn't exist
-
- Dim Path As String = "C:\Encrypt"
-
- ' the service has been started
-
- Protected Overrides Sub OnStart(ByVal args() As String)
- If args.Length > 0 Then
- Path = args(0)
- Me.EventLog.WriteEntry(Path)
- End If
- ' Ensure that the directory exists.
- If Not System.IO.Directory.Exists(Path) Then
- System.IO.Directory.CreateDirectory(Path)
- End If
- ' Start receiving file events.
- FileSystemWatcher1.Path = Path
- FileSystemWatcher1.EnableRaisingEvents = True
- End Sub
-
- ' the service has been stopped, paused, or resumed
-
- Protected Overrides Sub OnStop()
- ' Stop receiving file events.
- FileSystemWatcher1.EnableRaisingEvents = False
- End Sub
-
- Protected Overrides Sub OnPause()
- ' Stop receiving file events.
- FileSystemWatcher1.EnableRaisingEvents = False
- End Sub
-
- Protected Overrides Sub OnContinue()
- ' Start receiving file events.
- FileSystemWatcher1.EnableRaisingEvents = True
- End Sub
-
-
- ' This is the binary password.
- Dim pwBytes() As Byte = {123, 234, 12, 9, 78, 89, 212}
- ' This is the extension used for temporary files.
- Dim tempExt As String = ".$$$"
-
- Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
- ' Ignore temporary files created by the encryption process.
- If System.IO.Path.GetExtension(e.FullPath) = tempExt Then Exit Sub
-
- Do
- Try
- ' Encrypt the file being created.
- EncryptFile(e.FullPath, pwBytes)
- ' Exit if successful
- Exit Do
- Catch ex As Exception
- Dim attempts As Integer
- attempts += 1
- ' Make up to 10 attempts.
- If attempts > 10 Then Exit Sub
- ' Wait for a while and retry the operation.
- System.Threading.Thread.Sleep(1000)
- End Try
- Loop
-
- End Sub
-
- ' This is the encryption/decryption routine
-
- Private Sub EncryptFile(ByVal Filename As String, ByVal pwBytes() As Byte)
- ' This the the size of each input block.
- ' (Files must be decrypted using the same block size.)
- Const BLOCKSIZE = 8192
-
- ' Determine the name of the temporary file.
- Dim tempFile As String = Filename & tempExt
-
- ' Open the source file as a binary input stream.
- Dim inStream As New System.IO.FileStream(Filename, IO.FileMode.Open)
- ' Open the output temporary file as a binary input stream.
- Dim outStream As New System.IO.FileStream(tempFile, IO.FileMode.Create)
- ' Determine the number of bytes to read.
- Dim bytesLeft As Long = inStream.Length
- ' Prepare an input buffer.
- Dim buffer(BLOCKSIZE - 1) As Byte
-
- ' Loop until there are bytes to read.
- Do While bytesLeft > 0
- ' Read max 8K bytes at a time
- Dim bytesToRead As Long = Math.Min(BLOCKSIZE, bytesLeft)
- ' Read into the input buffer.
- inStream.Read(buffer, 0, bytesToRead)
- ' Encrypt this buffer.
- EncryptArray(buffer, pwBytes)
- ' Output to the temporary file.
- outStream.Write(buffer, 0, bytesToRead)
- ' We have fewer bytes to read now
- bytesLeft -= bytesToRead
- Loop
-
- ' Close the two streams.
- inStream.Close()
- outStream.Close()
- ' Delete the source file.
- System.IO.File.Delete(Filename)
- ' Rename the temporary file as the original file.
- System.IO.File.Move(tempFile, Filename)
- End Sub
-
- ' This routine encrypts an array of bytes.
- Sub EncryptArray(ByVal buffer() As Byte, ByVal pwBytes() As Byte)
- ' This index points to the buffer.
- Dim index As Integer
- ' This index points to the password array.
- Dim i As Integer
- ' The max value for i.
- Dim maxval As Integer = pwBytes.Length
-
- For index = 0 To buffer.Length - 1
- ' XOR each element in the buffer with the corresponding p
- buffer(index) = buffer(index) Xor pwBytes(i)
- ' ensure that the index is always in the valid range.
- i = (i + 1) Mod maxval
- Next
- End Sub
-
- ' This method is called only if the CanShutdown property is True.
-
- Protected Overrides Sub OnShutdown()
- ' Add here the code to execute when the system shuts down
- End Sub
-
- ' This method is called only if the CanHandlePowerEvent property is True.
-
- Protected Overrides Function OnPowerEvent(ByVal powerStatus As System.ServiceProcess.PowerBroadcastStatus) As Boolean
- Select Case powerStatus
- Case PowerBroadcastStatus.Suspend
- ' Add here the code to execute when the system enters suspend mode.
- Case PowerBroadcastStatus.ResumeSuspend
- ' Add here the code to execute when the system exits suspend mode.
- Case PowerBroadcastStatus.BatteryLow
- ' Add here the code to execute when batteries are low.
- End Select
- ' This method must return True.
- Return True
- End Function
-
- ' This method is called when a custom command is sent to the service.
-
- Protected Overrides Sub OnCustomCommand(ByVal command As Integer)
- Select Case command
- Case 1
- ' React to custom command #1
- Case 2
- ' React to custom command #1
- End Select
- End Sub
- End Class
-